﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;
using System.Windows.Data;

namespace V2TEK.NET.SH.Silverlight.Charts.TreeGridHelper
{
    /// <summary>
    /// 包含一个默认的Expander，继承自AbstractExpander。
    /// 如有必要，可以通过继承AbstractExpander，复写Expander，并设置ExpanderProvider。
    /// </summary>
    public class TreeGridExpanderColumn : DataGridTextColumn
    {
        public TreeGridExpanderColumn()
        {
        }

        private AbstractExpander CreateExpander()
        {
            return Activator.CreateInstance(ExpanderProvider) as AbstractExpander;        
        }

        public Type ExpanderProvider
        {
            get;
            set;
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            StackPanel panel = GeneratePanel(dataItem);
            panel.Children.Add(base.GenerateEditingElement(cell, dataItem));
            return panel;
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            StackPanel panel = GeneratePanel(dataItem);
            panel.Children.Add(base.GenerateElement(cell, dataItem));
            return panel;
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            TextBox ee = editingElement as TextBox;
            if (ee == null)
            {
                return string.Empty;
            }
            string text = ee.Text;
            int length = text.Length;
            KeyEventArgs args = editingEventArgs as KeyEventArgs;
            if ((args != null) && (args.Key == Key.F2))
            {
                ee.Select(length, length);
                return text;
            }
            ee.Select(0, length);

            return base.PrepareCellForEdit(editingElement, editingEventArgs);
        }

        public AbstractExpander GetExpander(DataGridRow row)
        {
            StackPanel panel = (StackPanel)this.GetCellContent(row);
            AbstractExpander expander = panel.Children[0] as AbstractExpander;
            expander.Data = row.DataContext;
            return expander;
        }

        private StackPanel GeneratePanel(object dataItem)
        {
            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;
            panel.Children.Add(GenerateExpander(dataItem));
            return panel;
        }

        private AbstractExpander GenerateExpander(object dataItem)
        {
            AbstractExpander expander = CreateExpander();
            expander.Data = dataItem;
            expander.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(expander_PropertyChanged);
            return expander;
        }

        void expander_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == AbstractExpander.DATA_PROPERTY)
                (sender as AbstractExpander).Update();
        }
    }
}
